﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="Logger.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace Common
{
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Threading;

    public class Logger : ILogger
    {
        private static readonly object LockObject = new object();

        private static Logger instance;

        public static Logger Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (LockObject)
                    {
                        if (instance == null)
                        {
                            var localInstance = new Logger();
                            Thread.MemoryBarrier();
                            instance = localInstance;
                        }
                    }
                }

                return instance;
            }
        }

        public void LogError(string errorMessage, [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0)
        {
            var output = string.Format("{0}:{1}:{2}:{3}", memberName, sourceLineNumber, DateTimeOffset.UtcNow, errorMessage);
            Trace.TraceError(output);
        }

        public void LogInformation(string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0)
        {
            var output = string.Format("{0}:{1}:{2}:{3}", memberName, sourceLineNumber, DateTimeOffset.UtcNow, message);
            Trace.TraceError(output);
        }

        public void LogException(Exception exception, [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0)
        {
            var output = string.Format("{0}:{1}:{2}:{3}", memberName, sourceLineNumber, DateTimeOffset.UtcNow, exception);
            Trace.TraceError(output);
        }
    }
}